<?php
namespace app\gly\controller;
/*  
 *   汉印云打印 http://user.hprtcloud.com
 */
 
//*必填*：登录管理后台的账号ID 
defined('UserID') or define('UserID', '635529');
//*必填*: 注册账号后生成的ApiKey
defined('ApiKey') or define('ApiKey', 'a0b7f155-0639-11ed-a4f5-98039b073928');
//*必填*：打印机编号，必须要在管理后台里手动添加打印机或者通过API添加之后，才能调用API,示例："hcs100121300XX";       
defined('PrinterNo') or define('PrinterNo', 'DP200020180110');
//*必填*：接口请求地址,请求方法:POST
defined('URL') or define('URL', 'http://api.hprtcloud.com/api/values');

class PrintApi {
	
	//添加打印机
	public function AddPrinter(){
		$PrinterNo = strtoupper(PrinterNo);
		//获取当前时间戳
		$time = time();
		$PostData = array(
			"UserID" => UserID,
			"ApiKey" => ApiKey,
			"TimeStamp" => $time,
			"PrinterNo" => $PrinterNo,
			//签名加密方法：用户ID+打印机编号+****当前时间戳****+接口密钥连接成字符串后进行md5加密
			"Sign" => md5(UserID.$PrinterNo.$time.ApiKey),
			"Fun" => "addprinter",
			"TerimalName" => "第一台测试云盒子",
			
		);
		//将数组转化为字符串，JSON_UNESCAPED_UNICODE 中文原码输出
		$post=json_encode($PostData,JSON_UNESCAPED_UNICODE);
		//提交json字串
		return self::curl(URL,$post,"POST");
	}
	
	//删除打印机
    public function DelPrinter(){
		$PrinterNo = strtoupper(PrinterNo);
		$time = time();
		$PostData = array(
			'PrinterNo' => $PrinterNo,
			'UserID' => UserID,
			'TerimalName' => '第一台测试云盒子',
			'Fun' => 'delprinter',
			'TimeStamp' => $time,
			'Sign' => md5(UserID.$PrinterNo.$time.ApiKey),
		);
		
		$post = json_encode($PostData,JSON_UNESCAPED_UNICODE);
		return self::curl(URL,$post,"POST");
	}
	
	//获取打印机状态
    public function GetPrinterStatus(){
		$PrinterNo = strtoupper(PrinterNo);
		$time = (string)time();
		$PostData = array(
			'PrinterNo' => $PrinterNo,
			'UserID' => UserID,
			'TerimalName' => '第一台测试云盒子',
			'Fun' => 'getprinterstatus',
			'TimeStamp' => $time,
			'Sign' => md5(UserID.$PrinterNo.$time.ApiKey),
		);
		
		$post = json_encode($PostData,JSON_UNESCAPED_UNICODE);
		return self::curl(URL,$post,"POST");
	}
	
	//查询打印完成情况
    public function QueryPrintComplete(){
		$PrinterNo = strtoupper(PrinterNo);
		$time = time();
		$PostData = array(
			'PrinterNo' => $PrinterNo,
			'UserID' => UserID,
			'TerimalName' => '第一台测试云盒子',
			'Fun' => 'queryprintcomplete',
			'PrintGuid' => 'ffe7e271-62f6-4e35-a079-c099b6a388ae',
			'TimeStamp' => $time,
			'Sign' => md5(UserID.$PrinterNo.$time.ApiKey),
		);
		$post = json_encode($PostData,JSON_UNESCAPED_UNICODE);
		return self::curl(URL,$post,"POST");
	}
	
	//打印ESC订单
    public function Printer(){
		//示例打印内容
		$orderInfo = "<H2>lee0333</H2><BR>";//标题字体如需居中放大,就需要用标签套上
		$orderInfo .= "名称　　　　　 单价  数量 金额<BR>";
		$orderInfo .= "--------------------------------<BR>";
		$orderInfo .= "青椒肉丝　　　 1.0    1   1.0<BR>";
		$orderInfo .= "西红柿炒蛋　　 10.0   10  100.0<BR>";
		$orderInfo .= "肉沫茄子　　　 10.0   1   10.0<BR>";
		$orderInfo .= "肉片汤  　　　 100.0  1   100.0<BR>";
		$orderInfo .= "咖喱鸡块  　　 100.0  2   200.0<BR>";
		$orderInfo .= "大盘鸡         100.0  1   100.0<BR>";
		$orderInfo .= "鱼蛋粉         15.0   1   15.0<BR>";
		$orderInfo .= "备注：不要辣<BR>";
		$orderInfo .= "--------------------------------<BR>";
		$orderInfo .= "合计：526.0元<BR>";
		$orderInfo .= "送货地点：xxxxxxxxxxxxxxxxx<BR>";
		$orderInfo .= "联系电话：XXXXXXXXXX<BR>";
		$orderInfo .= "订餐时间：2017-06-19 10:30:10<BR>";
		$orderInfo .= "----------请扫描二维码----------<BR>";
		$orderInfo .= "<QRCODE>http://www.hprt.com/</QRCODE>";//把二维码字符串用标签套上即可自动生成二维码
		$orderInfo .= "<BR>";
		
		$time = time();
		$PrinterNo = strtoupper(PrinterNo);
		$posdata = array(
			'PrintContent' => $orderInfo,
			'ApiKey' => ApiKey,
			'PrinterNo' => $PrinterNo,
			'TerimalName' => "第一台测试云盒子",
			'UserID' => UserID,
			'PrinterOrderSet' => "ESC",
			'Fun' => "print",
			'TimeStamp' => $time,
			'Sign' => md5(UserID.$PrinterNo.$time.ApiKey),
		);
		$posdata = json_encode($posdata,JSON_UNESCAPED_UNICODE);
		return self::curl(URL,$posdata,"POST");
			
	}

    //打印TSPL订单
    public function PrinterTspl(){
        //示例打印内容
        // 在TSPL指令中<Rn/>指换行，不可使用<Br>进行换行 无法识别
        $orderInfo  = 'SIZE 57mm,100mm<Rn/>';
        $orderInfo .= 'GAP 2 mm,0 mm<Rn/>';
        $orderInfo .= 'CLS<Rn/>';
        $orderInfo .= 'TEXT 25,30,"9",0,72,72,"顺丰快递（机场）"<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= 'BOLD 1<Rn/>';
        $orderInfo .= 'TEXT 88,152,"9",0,36,48,"100-200-30-400"<Rn/>';
        $orderInfo .= 'TEXT 88,260,"9",0,32,32,"区域件"<Rn/>';
        $orderInfo .= 'BARCODE 480,251,"128",48,0,0,4,4,"123456"<Rn/>';
        $orderInfo .= 'TEXT 24,362,"9",0,36,36,"收"<Rn/>';
        $orderInfo .= 'TEXT 88,328,"9",0,24,24,"厦门汉印电子有限公司   18099980909"<Rn/>';
        $orderInfo .= 'TEXT 88,368,"9",0,24,24,"福建省厦门市湖里区殿前街道高崎南十二路艾德航空工业园八号"<Rn/>';
        $orderInfo .= 'TEXT 88,400,"9",0,24,24,"楼五楼前台"<Rn/>';
        $orderInfo .= 'TEXT 24,479,"9",0,36,36,"寄"<Rn/>';
        $orderInfo .= 'TEXT 24,1040,"9",0,36,36,"收"<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= 'TEXT 24,1168,"9",0,36,36,"寄"<Rn/>';
        $orderInfo .= 'BARCODE 88,590,"128",112,2,0,2,5,"YT200012345607"<Rn/>';
        $orderInfo .= 'BARCODE 424,920,"128",40,2,0,2,2,"YT200012345608"<Rn/>';
        $orderInfo .= 'TEXT 660,880,"9",0,24,24,"签收栏"<Rn/>';
        $orderInfo .= 'TEXT 88,456,"9",0,24,24,"厦门汉印电子有限公司  18099980909"<Rn/>';
        $orderInfo .= 'TEXT 88,496,"9",0,24,24,"福建省厦门市湖里区殿前街道高崎南十二路艾德航空工业园八号"<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= 'TEXT 32,760,"9",0,24,24,"2019/06/25"<Rn/>';
        $orderInfo .= 'TEXT 37,795,"9",0,24,24,"12:55:01"<Rn/>';
        $orderInfo .= 'TEXT 35,835,"9",0,24,24,"打印时间"<Rn/>';
        $orderInfo .= 'TEXT 35,870,"9",0,24,24,"始发网点"<Rn/>';
        $orderInfo .= 'QRCODE 180,752,L,5,A,0,M2,S7,"http://www.hprtcloud.com"<Rn/>';
        $orderInfo .= 'TEXT 353,765,"9",0,16,16,"快件送达收件人地址，经收件人或收件人（寄件人）"<Rn/>';
        $orderInfo .= 'TEXT 353,789,"9",0,16,16,"允许的代收人签字，视为送达。您的签字代表您已验"<Rn/>';
        $orderInfo .= 'TEXT 353,813,"9",0,16,16,"收货此包裹，并已确认商品信息无误、包裹完整、没有"<Rn/>';
        $orderInfo .= 'TEXT 353,837,"9",0,16,16,"划痕、破损等表面质量问题。"<Rn/>';
        $orderInfo .= 'TEXT 50,920,\"9\",0,72,72,\"顺丰快递\"<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= 'TEXT 88,1016,"9",0,24,24,"厦门汉印电子有限公司   18099980909"<Rn/>';
        $orderInfo .= 'TEXT 88,1040,"9",0,24,24,"福建省厦门市湖里区殿前街道高崎南十二路艾德航空工业园八号"<Rn/>';
        $orderInfo .= 'TEXT 88,400,"9",0,24,24,"楼五楼前台"<Rn/>';
        $orderInfo .= 'TEXT 88,1144,"9",0,24,24,"厦门汉印电子有限公司  18099980909"<Rn/>';
        $orderInfo .= 'TEXT 88,1168,"9",0,24,24,"福建省厦门市湖里区殿前街道高崎南十二路艾德航空工业园八号"<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= 'TEXT 32,1255,"9",0,24,24,"订单号：K1234567890"<Rn/>';
        $orderInfo .= 'BAR 0,112,800,2<Rn/>';
        $orderInfo .= 'BAR 0,240,800,2<Rn/>';
        $orderInfo .= 'BAR 0,312,800,2<Rn/>';
        $orderInfo .= 'BAR 0,440,800,2<Rn/>';
        $orderInfo .= 'BAR 0,568,800,2<Rn/>';
        $orderInfo .= 'BAR 0,744,800,2<Rn/>';
        $orderInfo .= 'BAR 0,910,800,4<Rn/>';
        $orderInfo .= 'BAR 0,990,800,2<Rn/>';
        $orderInfo .= 'BAR 0,1118,800,2<Rn/>';
        $orderInfo .= 'BAR 0,1246,800,2<Rn/>';
        $orderInfo .= 'BAR 168,744,1,168<Rn/>';
        $orderInfo .= 'BAR 340,744,1,168<Rn/>';
        $orderInfo .= 'PRINT 1,1<Rn/>';
        $orderInfo .= '<Rn/>';
        halt($orderInfo);
        $time = time();
        $PrinterNo = strtoupper(PrinterNo);
        $posdata = array(
            'PrintContent' => $orderInfo,
            'ApiKey' => ApiKey,
            'PrinterNo' => $PrinterNo,
            'UserID' => UserID,
            'PrinterOrderSet' => "TSPL",
            'Fun' => "print",
            'TimeStamp' => $time,
            'Sign' => md5(UserID.$PrinterNo.$time.ApiKey),
            'PrintCount'=>1
        );

        $posdata = json_encode($posdata,JSON_UNESCAPED_UNICODE);

        return self::curl(URL,$posdata,"POST");

    }

    //打印TSPL位图
    public function PrintBitmapTspl(){
        //默认测试文件，可自定义更改，仅支持jpg格式
        $testfile = "../img/invoice.png";
        //获取图片内容
        $imgData = file_get_contents($testfile);
        if(file_exists($testfile)){
            $image = base64_encode($imgData);//进行base64编码
        }else{
            echo "找不到图片";
            exit;
        }
        $orderInfo = "<image>".$image."</image><cut>";
        $PrinterNo = strtoupper(PrinterNo);
        $time = time();
        $posData = array(
            "PrintContent" => $orderInfo,
            "ApiKey" => ApiKey,
            "PrinterNo" => $PrinterNo,
            "UserID" => UserID,
            "PrinterOrderSet" => "TSPL",
            "Fun" => "print",
            "TimeStamp" => $time,
            "Sign" => md5(UserID.$PrinterNo.$time.ApiKey)
        )	;
        $posData = json_encode($posData,JSON_UNESCAPED_UNICODE);

        return self::curl(URL,$posData,"POST");
    }

	//打印ESC位图
    public function PrintBitmap(){
		//默认测试文件，可自定义更改，仅支持jpg格式
		$testfile = "../img/liantu3.jpg";
		//获取图片内容
		$imgData = file_get_contents($testfile);
		if(file_exists($testfile)){
			$image = base64_encode($imgData);//进行base64编码
		}else{
			echo "找不到图片";
			exit;
		}
		$orderInfo = "<image>".$image."</image><BR><BR><BR><cut>";
		$PrinterNo = strtoupper(PrinterNo);
		$time = time();
		$posData = array(
			"PrintContent" => $orderInfo,
			"ApiKey" => ApiKey,
			"PrinterNo" => $PrinterNo,
			"TerimalName" => "第一台测试云盒子",
			"UserID" => UserID,
			"PrinterOrderSet" => "ESC",
			"Fun" => "print",
			"TimeStamp" => $time,
			"Sign" => md5(UserID.$PrinterNo.$time.ApiKey)
		)	;
		$posData = json_encode($posData,JSON_UNESCAPED_UNICODE);
		echo $posData;
		return self::curl(URL,$posData,"POST");
	}

	//请求函数
    public function curl($url,$data,$request="POST"){
		$ch=curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, 8);  
		curl_setopt($ch, CURLOPT_URL, $url);  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
			'Content-Type:application/json' //定义上传请求头的内容类型为json字符串
		));                    
		$result = curl_exec($ch);
		$decode_result=json_decode($result,true);
		return $decode_result;
		curl_close($ch);
	}

    //打印TSPL订单
    public function PrinterOrder($data=[]){
        //示例打印内容
        // 在TSPL指令中<Rn/>指换行，不可使用<Br>进行换行 无法识别
        $orderInfo  = 'SIZE 57mm,80mm<Rn/>';
        $orderInfo .= 'GAP 1 mm,0 mm<Rn/>';
        $orderInfo .= 'CLS<Rn/>';
        $orderInfo .= 'TEXT 70,30,"9",0,60,60,"双虹唛头纸"<Rn/>';
        $orderInfo .= '<Rn/>';
        $orderInfo .= 'TEXT 30,120,"9",0,30,30,"数量/总数量：'.$data['num'].'"<Rn/>';
        $orderInfo .= 'TEXT 30,170,"9",0,30,30,"Po/货号：'.$data['po'].'"<Rn/>';
        $orderInfo .= 'TEXT 30,220,"9",0,30,30,"日期：'.$data['date'].'"<Rn/>';
        $orderInfo .= 'TEXT 30,270,"9",0,30,30,"位置：'.$data['address'].'"<Rn/>';
        $orderInfo .= 'QRCODE 150,400,L,5,A,0,M2,S7,"http://sh.xcwzy.cn/ruku_sure.html?id='.$data['id'].'"<Rn/>';
        $orderInfo .= 'PRINT 1,1<Rn/>';
        //halt($orderInfo);
        /*$orderInfo  = '<h3>物流打印</h3><Rn/>';
        $orderInfo .= '<Left>数量：10</Left><Rn/>';
        $orderInfo .= '<Left>Po：******</Left><Rn/>';
        $orderInfo .= '<Left>日期：2022-07-18</Left><Rn/>';
        $orderInfo .= '<Left>位置：河南省郑州市管城区方圆创世A座1713</Left> <Rn/>';
        $orderInfo .= '<Center><QRCode Width=5>https://www.baidu.com/</QRCode></Center><Rn/>';
        $orderInfo .= '<Rn/>';*/

        $time = time();
        $PrinterNo = strtoupper(PrinterNo);
        $posdata = array(
            'PrintContent' => $orderInfo,
            'ApiKey' => ApiKey,
            'PrinterNo' => $PrinterNo,
            'UserID' => UserID,
            'PrinterOrderSet' => "TSPL",
            'Fun' => "print",
            'TimeStamp' => $time,
            'Sign' => md5(UserID.$PrinterNo.$time.ApiKey),
            'PrintCount'=>1
        );

        $posdata = json_encode($posdata,JSON_UNESCAPED_UNICODE);

        return self::curl(URL,$posdata,"POST");

    }

	public function printTest(){
		//$res1=$this->GetPrinterStatus();//查询打印机状态
		//$res2=$this->Printer();//打印ESC订单
		//$res4=$this->PrinterTspl();	//打印TSPL订单
		/*$res3=$this->QueryPrintComplete();//查询打印完成情况
		/*echo "查询打印机状态:";
		dump($res1);
		/*echo "打印ESC订单:";
		dump($res2);
		echo "查询打印完成情况:";
		dump($res3);
		echo "打印TSPL订单:";*/
		$res=$this->PrinterOrder();	//打印TSPL订单
		dump($res);//dump($res4);
	}

}

/*$test = new PrintApi;
// $test -> AddPrinter();
var_dump($test->PrintBitmapTspl());*/

?>